﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using EzDesk.Utility.EzML;
using EzDesk.Core.Model;
using EzDesk.Utility;

namespace EzDesk.Core.CoreImpl
{
    public partial class CoreClientService : ICoreClientService
    {

        public string GetXmlFeedsBetweenDate(string feedTypeStr, Guid userId, DateTime from, DateTime to, bool? isRead, bool? isAvaiable)
        {
            var feeds = _coreService.GetFeeds(feedTypeStr, userId, from, to, 50, isRead, isAvaiable);
            return generateFeedsXml(feeds, feedTypeStr, userId);
        }

        public string GetXmlFeedsByCount(string feedTypeStr, Guid userId, int startIndex, int count, bool? isRead, bool? isAvaiable)
        {
            var feeds = _coreService.GetFeeds(feedTypeStr, userId, startIndex, count, isRead, isAvaiable);
            return generateFeedsXml(feeds, feedTypeStr, userId);
        }


        private string generateFeedsXml(IList<Feed> feeds, string feedTypeStr, Guid userId)
        {
            XElement feedItems = new XElement("feeditems");

            XElement root = new XElement("root");
            root.Add(new XAttribute("appportalurl", "http://" + _coreService.AppPortalUrl + "/page"));
            root.Add(feedItems);

            int count = 0;
            
            foreach (var feed in feeds)
            {
                string feedDataStr = feed.FeedData;
                IDictionary<string, string> originalfeedData =
                        JsonHelper.JsonToDictionary(feed.FeedData);

                IDictionary<string, string> extraFeedData =
                    JsonHelper.JsonToDictionary(_coreService.GetPersonalFeedData(userId, feed.Id));
                if (extraFeedData != null && extraFeedData.Count > 0)
                {
                    // 合并数据
                    foreach (var keyPair in extraFeedData)
                    {
                        if (originalfeedData.ContainsKey(keyPair.Key))
                        {
                            originalfeedData[keyPair.Key] = keyPair.Value;
                        }
                        else
                        {
                            originalfeedData.Add(keyPair);
                        }
                    }

                    feedDataStr = JsonHelper.ConvertToJson(originalfeedData);
                }

                var feedTemplate = _coreService.GetFeedTemplateById(feed.TemplateId);
                var app = _coreService.GetAppById(feedTemplate.AppId);

                string feedContent = EzMLHelper.MergeDataToEzML(
                    feedTemplate.Title + feedTemplate.Body,
                    originalfeedData
                    );

                XElement feedItem = new XElement("feeditem");
                feedItem.Add(new XAttribute("appname", app.Name));
                feedItem.Add(new XAttribute("guid", feed.Id.ToString()));
                feedItem.Add(new XAttribute("createon", feed.CreateOn));
                feedItem.Add(new XAttribute("updateon", feed.UpdateOn));
                feedItem.Add(new XAttribute("type", feedTypeStr));
                feedItem.Add(new XAttribute("isread", feed.IsRead));
                feedItem.Add(new XAttribute("isavailable", feed.IsAvailable));
                feedItem.Add(new XAttribute("lastchangeon", feed.LastChangeOn));

                feedItem.Add(new XElement("data", new XCData(feedDataStr)));

                feedItem.Add(new XElement("content", new XCData(feedContent)));

                feedItems.Add(feedItem);
                count++;
            }

            feedItems.Add(new XAttribute("count", count));

            return root.ToString(); ;
                        
        }




        public void SetFeedIsRead(Guid feedId, string memberString, bool isRead)
        {
            _coreService.SetFeedIsRead(feedId, memberString, isRead);
        }


        public void SetFeedsIsRead(string feedIdStrs, string memberString, bool isRead)
        {
            _coreService.SetFeedIsAvailable(feedIdStrs, memberString, isRead);
        }
    }
}
